home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15537 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: presby.edu!jtbell
  3. From: jtbell@presby.edu (Jon Bell)
  4. Subject: Re: What are the differences between structures and classes in C++ ?
  5. Message-ID: <DpG53J.Hsz@presby.edu>
  6. Date: Sat, 6 Apr 1996 15:16:31 GMT
  7. References: <4k5m65$av@hpscit.sc.hp.com>
  8. Organization: Presbyterian College, Clinton, South Carolina USA
  9.  
  10.  Raghuveera Ravinutala  <raghur> wrote:
  11. >     Please mail me the differences between structures and classes in C++.
  12. >Raghu.
  13.  
  14. Sorry, I can't mail this to you because you didn't provide a complete 
  15. e-mail address.  But I'll post it anyway...
  16.  
  17. As far as I can tell, the only *formal* difference between a class and a
  18. struct is that by default, class members are private whereas struct
  19. members are public.  Classes and structs can each contain both member data
  20. and member functions.  Therefore, the following declarations are all
  21. equivalent: 
  22.  
  23. // version 1
  24.  
  25.     class Foo
  26.     {
  27.        double Foodata;
  28.     public:
  29.        double GetFoodata (void) const;
  30.        void SetFoodata (double);
  31.     };
  32.  
  33. // version 2
  34.  
  35.     class Foo
  36.     {
  37.     private:
  38.        double Foodata;
  39.     public:
  40.        double GetFoodata (void) const;
  41.        void SetFoodata (double);
  42.     };
  43.  
  44. // version 3
  45.  
  46.     struct Foo
  47.     {
  48.        double GetFoodata (void) const;
  49.        void SetFoodata (double);
  50.     private:
  51.        double Foodata;
  52.     };
  53.  
  54. // version 4
  55.  
  56.     struct Foo
  57.     {
  58.     public:
  59.        double GetFoodata (void) const;
  60.        void SetFoodata (double);
  61.     private:
  62.        double Foodata;
  63.     };
  64.  
  65. Everybody uses classes for a situation like this one, rather than 
  66. structs, but I haven't found anything (yet) in Stroustrup's "The C++ 
  67. Programming Language" that would forbid using a struct instead.
  68.  
  69. -- 
  70. Jon Bell <jtbell@presby.edu>                        Presbyterian College
  71. Dept. of Physics and Computer Science        Clinton, South Carolina USA
  72.